/*
 * @Author: secrets123
 * @Date: 2022-07-21 10:53:28
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-08-22 17:03:54
 * @FilePath: \stm32g431_AllRings\applications\srt_im.c
 * @Description:
 *
 * Copyright (c) 2022 by secrets123 1097290967@qq.com, All Rights Reserved.
 */
#include <rtthread.h>
#include <rtconfig.h>
#include <board.h>
#include "rs485.h"
#include "srt_im.h"
#include <agile_modbus.h>
// #include "allrings_sys.h"

#define DBG_TAG "rtu_master"
#define DBG_LVL DBG_INFO // DBG_ERROR // DBG_INFO
#include <rtdbg.h>

struct _srt_ctr srt_im =
    {
        .srt_im_values.addr = 0X01,

        /*SRT-IM设备*/
        /*01*/
        .device_cmd_srtim[_SRT_IM_R_COILS_CLEAR_CTR] = SRT_IM_R_COILS_CLEAR_CTR,
        .device_cmd_srtim[_SRT_IM_R_COILS_CLEAR_ERR] = SRT_IM_R_COILS_CLEAR_ERR,
        .device_cmd_srtim[_SRT_IM_R_COILS_CALIBRATION_ZERO] = SRT_IM_R_COILS_CALIBRATION_ZERO,

        /*02*/
        .device_cmd_srtim[_SRT_IM_R_DISINPUTSREG_P_IN_BIT] = SRT_IM_R_DISINPUTSREG_P_IN_BIT,
        .device_cmd_srtim[_SRT_IM_R_DISINPUTSREG_N_IN_BIT] = SRT_IM_R_DISINPUTSREG_N_IN_BIT,
        .device_cmd_srtim[_SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT] = SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT,
        .device_cmd_srtim[_SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT] = SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT,

        /*04*/
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_RTP] = SRT_IM_R_INPUTREG_RTP,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_CTP] = SRT_IM_R_INPUTREG_CTP,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_CTP_SAVE] = SRT_IM_R_INPUTREG_CTP_SAVE,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_POS_CONFIG] = SRT_IM_R_INPUTREG_POS_CONFIG,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_NEG_CONFIG] = SRT_IM_R_INPUTREG_NEG_CONFIG,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_NOW_TRG] = SRT_IM_R_INPUTREG_NOW_TRG,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_END_TRG] = SRT_IM_R_INPUTREG_END_TRG,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_ERR_BIT] = SRT_IM_R_INPUTREG_ERR_BIT,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_HARD_VER] = SRT_IM_R_INPUTREG_HARD_VER,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_SOFTWARE_VER] = SRT_IM_R_INPUTREG_SOFTWARE_VER,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_TRG_GEAR] = SRT_IM_R_INPUTREG_TRG_GEAR,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_POS_ADJUST_MAX] = SRT_IM_R_INPUTREG_POS_ADJUST_MAX,
        .device_cmd_srtim[_SRT_IM_R_INPUTREG_NEG_ADJUST_MAX] = SRT_IM_R_INPUTREG_NEG_ADJUST_MAX,

        /*03*/
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_RTU_ID] = SRT_IM_R_KEEPREG_RTU_ID,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_TRG] = SRT_IM_R_KEEPREG_TRG,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_CYC_TIME] = SRT_IM_R_KEEPREG_CYC_TIME,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_TS_TIME] = SRT_IM_R_KEEPREG_TS_TIME,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_POS_SET] = SRT_IM_R_KEEPREG_POS_SET,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_NEG_SET] = SRT_IM_R_KEEPREG_NEG_SET,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_GEAR] = SRT_IM_R_KEEPREG_GEAR,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_IAP_PASS] = SRT_IM_R_KEEPREG_IAP_PASS,
        .device_cmd_srtim[_SRT_IM_R_KEEPREG_CAIL_ZERO] = SRT_IM_R_KEEPREG_CAIL_ZERO,

        /*06-10*/
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_RTU_ID] = SRT_IM_W_KEEPREG_RTU_ID,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_TRG] = SRT_IM_W_KEEPREG_TRG,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_CYC_TIME] = SRT_IM_W_KEEPREG_CYC_TIME,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_TS_TIME] = SRT_IM_W_KEEPREG_TS_TIME,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_POS_SET] = SRT_IM_W_KEEPREG_POS_SET,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_NEG_SET] = SRT_IM_W_KEEPREG_NEG_SET,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_GEAR] = SRT_IM_W_KEEPREG_GEAR,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_IAP_PASS] = SRT_IM_W_KEEPREG_IAP_PASS,
        .device_cmd_srtim[_SRT_IM_W_KEEPREG_CAIL_ZERO] = SRT_IM_W_KEEPREG_CAIL_ZERO,

        /*指令合法性检查*/
        .inputcmd_valid_check = srt_cmd_check,
        /*CAN 数据反馈接口*/
        .can_sendback_data = fdcan_send2up_data,
};

// srt-注册指令检查
uint8_t srt_cmd_check(uint8_t cmdcheck)
{
    uint8_t sta = 0;
    switch (cmdcheck)
    {

#define X_MACRO(a, b) case a:
        SRT_CMD_LIST_TAB
#undef X_MACRO
        sta = 1;
        break;
    default:
        sta = 0;
        break;
    }
    return sta;
}
// CAN反馈到上层
void fdcan_send2up_data(struct _fdcan_rxdata *fdcandata)
{
    uint16_t id;
    uint8_t length;
    uint8_t *data;

    id = fdcandata->id;
    length = fdcandata->dta_length;
    data = fdcandata->data;
    FDCAN_Transmit(&hfdcan1, id, length, fdcandata->data);
}

/**********************************SRT-CMD 接口命令********************************/
static void srt_test_show(int rc, uint8_t cmd, uint8_t *Register)
{
    if (rc < 0)
    {
        LOG_W("Receive failed.");
        if (rc != -1)
            LOG_W("Error code:%d", -128 - rc);
    }
    else
    {
        // LOG_I("Coils Registers:");
        for (int i = 0; i < rc; i++)
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", cmd, i, Register[i]);
    }
}

void srt_can_packed_u8(struct _srt_ctr *srt, uint8_t cmd, uint8_t *data, uint8_t datnum)
{
    if (cmd == DEVICE_RS485_ERR_TIMEOUT)
    {
        if (++srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter >= DEVICE_RS485_ERR_THRESHOLD)
        {
            srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter = 0;
            rings_ctr.allrings_values.allsys_err.bit.RS485_TIMEOUT = 1;
        }
    }
    else if (cmd == DEVICE_RS485_ERR_FAILURE)
    {
        if (++srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter >= DEVICE_RS485_ERR_THRESHOLD)
        {
            srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter = 0;
            rings_ctr.allrings_values.allsys_err.bit.RS485_ERR = 1;
        }
    }
    else
    {
        srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter = 0;
        srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter = 0;
        rings_ctr.allrings_values.allsys_err.bit.RS485_TIMEOUT = 1;

        srt->fdcan_fdback.data[0] = cmd;

        if (data == NULL)
        {
            srt->fdcan_fdback.dta_length = 1;
        }
        else
        {
            for (uint16_t i = 0; i < datnum; i++)
            {
                srt->fdcan_fdback.data[1 + i] = data[i];
            }
            // memcpy(&srt->fdcan_fdback.data[3], data, datnum);
            srt->fdcan_fdback.dta_length = 1 + datnum;
        }
        srt->can_sendback_data(&srt->fdcan_fdback);
    }
}
void srt_can_packed_u16(struct _srt_ctr *srt, uint8_t cmd, uint16_t *data, uint8_t datnum)
{
    if (cmd == DEVICE_RS485_ERR_TIMEOUT)
    {
        if (++srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter >= DEVICE_RS485_ERR_THRESHOLD)
        {
            srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter = 0;
            rings_ctr.allrings_values.allsys_err.bit.RS485_TIMEOUT = 1;
        }
    }
    else if (cmd == DEVICE_RS485_ERR_FAILURE)
    {
        if (++srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter >= DEVICE_RS485_ERR_THRESHOLD)
        {
            srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter = 0;
            rings_ctr.allrings_values.allsys_err.bit.RS485_ERR = 1;
        }
    }
    else
    {
        srt->srt_im_values.srt_com_err_judge.rs485_err_timeout_counter = 0;
        srt->srt_im_values.srt_com_err_judge.rs485_err_failure_counter = 0;
        rings_ctr.allrings_values.allsys_err.bit.RS485_TIMEOUT = 0;

        srt->fdcan_fdback.data[0] = cmd;
        if (data == NULL)
        {
            srt->fdcan_fdback.dta_length = 1;
        }
        else
        {
            switch (cmd)
            {
            case 0x06: // 特殊方式，为了兼容上位机
                srt->fdcan_fdback.data[1] = (uint8_t)data[0];
                srt->fdcan_fdback.data[2] = 0x00;
                srt->fdcan_fdback.data[3] = 0x00;
                srt->fdcan_fdback.data[4] = 0x00;
                srt->fdcan_fdback.dta_length = 5;
                break;
            case 0x0a: // 特殊方式，为了兼容上位机
                srt->fdcan_fdback.data[1] = 0x00;
                srt->fdcan_fdback.dta_length = 2;
                break;

            default:
                for (uint16_t i = 0; i < datnum; i++)
                {
                    srt->fdcan_fdback.data[1 + i * 2] = (uint8_t)(data[i] >> 8);
                    srt->fdcan_fdback.data[2 + i * 2] = (uint8_t)data[i];
                }
                // memcpy(&srt->fdcan_fdback.data[3], data, datnum);
                srt->fdcan_fdback.dta_length = 1 + datnum * 2;
                break;
            }
        }
        srt->can_sendback_data(&srt->fdcan_fdback);
    }
}
/*********************************************************************************************/
/********读线圈寄存器数据-01*******/
uint8_t SRT_IM_R_COILS_CLEAR_CTR(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_bits(ctx, REG_CLEAR_CTP, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 01, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_COILS_CLEAR_CTR, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_COILS_CLEAR_ERR(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_bits(ctx, REG_CLEAR_ERR, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 01, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_COILS_CLEAR_ERR, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_COILS_CALIBRATION_ZERO(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_bits(ctx, REG_CALIBRATION_ZERO, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 01, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_COILS_CALIBRATION_ZERO, Register, rc);
        }
    }
    return 0;
}

/********读离散输入寄存器数据-02*******/
uint8_t SRT_IM_R_DISINPUTSREG_P_IN_BIT(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_input_bits(ctx, REG_P_IN_BIT, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_input_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 02, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_DISINPUTSREG_P_IN_BIT, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_DISINPUTSREG_N_IN_BIT(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_input_bits(ctx, REG_N_IN_BIT, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_input_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 02, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_DISINPUTSREG_N_IN_BIT, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_input_bits(ctx, REG_KEYP_IN_BIT, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_input_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 02, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_DISINPUTSREG_KEYP_IN_BIT, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT(struct _srt_ctr *srt)
{
    int rc;
    uint8_t Register[10];

    int send_len = agile_modbus_serialize_read_input_bits(ctx, REG_KEYN_IN_BIT, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        srt_can_packed_u8(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        rc = agile_modbus_deserialize_read_input_bits(ctx, read_len, Register);
        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u8(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 02, i, Register[i]);

            srt_can_packed_u8(srt, _SRT_IM_R_DISINPUTSREG_KEYN_IN_BIT, Register, rc);
        }
    }
    return 0;
}
/********读保持寄存器数据-03*******/
uint8_t SRT_IM_R_KEEPREG_RTU_ID(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, RTU_ID, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_RTU_ID, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_TRG(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, TRG, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_TRG, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_CYC_TIME(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, CYC_TIME, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_CYC_TIME, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_TS_TIME(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, TS_TIME, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_TS_TIME, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_POS_SET(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, POS_SET, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_POS_SET, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_NEG_SET(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, NEG_SET, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_NEG_SET, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_GEAR(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, GEAR, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_GEAR, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_IAP_PASS(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, IAP_PASS, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_IAP_PASS, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_KEEPREG_CAIL_ZERO(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_registers(ctx, CAIL_ZERO, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 03, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_CAIL_ZERO, Register, rc);
        }
    }
    return 0;
}

/********读输入寄存器数据-04*******/
uint8_t SRT_IM_R_INPUTREG_RTP(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_RTP, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_RTP, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_CTP(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_CTP, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_CTP, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_CTP_SAVE(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_CTP_SAVE, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_CTP_SAVE, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_POS_CONFIG(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_POS_CONFIG, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_POS_CONFIG, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_NEG_CONFIG(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_NEG_CONFIG, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_NEG_CONFIG, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_NOW_TRG(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_NOW_TRG, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 04, i, Register[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_NOW_TRG, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_END_TRG(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_END_TRG, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_END_TRG, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_ERR_BIT(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_ERR_BIT, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_ERR_BIT, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_HARD_VER(struct _srt_ctr *srt)
{
    uint16_t Register[10];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_HARD_VER, 2);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_HARD_VER, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_SOFTWARE_VER(struct _srt_ctr *srt)
{
    uint16_t Register[30];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_SOFTWARE_VER, 12);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            // srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_SOFTWARE_VER, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_TRG_GEAR(struct _srt_ctr *srt)
{
    uint16_t Register[30];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_TRG_GEAR, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_TRG_GEAR, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_POS_ADJUST_MAX(struct _srt_ctr *srt)
{
    uint16_t Register[30];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_POS_ADJUST_MAX, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_POS_ADJUST_MAX, Register, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_R_INPUTREG_NEG_ADJUST_MAX(struct _srt_ctr *srt)
{
    uint16_t Register[30];

    int send_len = agile_modbus_serialize_read_input_registers(ctx, REG_NEG_ADJUST_MAX, 1);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_read_input_registers(ctx, read_len, Register);
        // srt_test_show(rc, 0x04, (uint8_t *)Register);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            srt_can_packed_u16(srt, _SRT_IM_R_INPUTREG_NEG_ADJUST_MAX, Register, rc);
        }
    }
    return 0;
}

/*******写单个线圈寄存器-05*******/
// uint8_t SRT_IM_W_COILS_CLEAR_CTR(struct _srt_ctr *srt)
// {
// }

/*******写保持寄存器-06(单个)-10（连续多个）*******/
uint8_t SRT_IM_W_KEEPREG_RTU_ID(struct _srt_ctr *srt)
{
    uint16_t values = 0;
    uint16_t Register[10];

    values = srt->fdcan_fdback.data[1] << 8 | srt->fdcan_fdback.data[2]; // 获取设置值

    int send_len = agile_modbus_serialize_write_register(ctx, RTU_ID, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_register(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            // for (int i = 0; i < rc; i++)
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt->srt_im_values.addr = values;
            agile_modbus_set_slave(ctx, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_RTU_ID, Register, 1);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_TRG(struct _srt_ctr *srt)
{
    uint16_t values = 0;
    // uint16_t Register[10];
    switch (srt->fdcan_fdback.data[1])
    {
    case 00:
        values = 0x0000;
        break;
    case 01:
        values = 0x0001;
        break;
    case 02:
        values = 0x0002;
        break;
    case 03:
        values = 0x0003;
        break;
    case 04:
        values = 0x0004;
        break;
    default:
        values = 0x0000;
        break;
    }
    // values = srt->fdcan_fdback.data[2] << 8 | srt->fdcan_fdback.data[3]; // 获取设置值

    int send_len = agile_modbus_serialize_write_register(ctx, TRG, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_register(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_TRG, &values, 1);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_CYC_TIME(struct _srt_ctr *srt)
{

    uint16_t values[2] = {0};
    // uint16_t Register[10];

    if (srt->fdcan_fdback.dta_length < 6)
    {
        return 0;
    }
    uint8_t vdta_length = (srt->fdcan_fdback.dta_length - 2) / 2;
    for (uint8_t i = 0; i < vdta_length; i++)
    {
        values[i] = srt->fdcan_fdback.data[1 + i] << 8 | srt->fdcan_fdback.data[2 + i]; // 获取设置值
    }

    int send_len = agile_modbus_serialize_write_registers(ctx, CYC_TIME, 2, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_registers(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 10, i, values[i]);

            srt_can_packed_u16(srt, _SRT_IM_R_KEEPREG_CYC_TIME, values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_TS_TIME(struct _srt_ctr *srt)
{

    uint16_t values[2] = {0};
    // uint16_t Register[10];

    if (srt->fdcan_fdback.dta_length < 6)
    {
        return 0;
    }
    uint8_t vdta_length = (srt->fdcan_fdback.dta_length - 2) / 2;
    for (uint8_t i = 0; i < vdta_length; i++)
    {
        values[i] = srt->fdcan_fdback.data[1 + i] << 8 | srt->fdcan_fdback.data[2 + i]; // 获取设置值
    }

    int send_len = agile_modbus_serialize_write_registers(ctx, TS_TIME, 2, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_registers(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            for (int i = 0; i < rc; i++)
                LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 10, i, values[i]);

            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_TS_TIME, values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_POS_SET(struct _srt_ctr *srt)
{
    uint16_t values = 0;
    // uint16_t Register[10];

    values = srt->fdcan_fdback.data[1] << 8 | srt->fdcan_fdback.data[2]; // 获取设置值

    int send_len = agile_modbus_serialize_write_register(ctx, POS_SET, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_register(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_POS_SET, &values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_NEG_SET(struct _srt_ctr *srt)
{
    uint16_t values = 0;
    // uint16_t Register[10];

    values = srt->fdcan_fdback.data[1] << 8 | srt->fdcan_fdback.data[2]; // 获取设置值

    int send_len = agile_modbus_serialize_write_register(ctx, NEG_SET, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_register(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_NEG_SET, &values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_GEAR(struct _srt_ctr *srt)
{
    uint16_t values = 0;
    // uint16_t Register[10];

    values = srt->fdcan_fdback.data[1] << 8 | srt->fdcan_fdback.data[2]; // 获取设置值

    int send_len = agile_modbus_serialize_write_register(ctx, GEAR, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_register(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_GEAR, &values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_IAP_PASS(struct _srt_ctr *srt)
{

    uint16_t values[2] = {0};
    // uint16_t Register[10];

    if (srt->fdcan_fdback.dta_length < 6)
    {
        return 0;
    }
    uint8_t vdta_length = (srt->fdcan_fdback.dta_length - 2) / 2;
    for (uint8_t i = 0; i < vdta_length; i++)
    {
        values[i] = srt->fdcan_fdback.data[1 + i] << 8 | srt->fdcan_fdback.data[2 + i]; // 获取设置值
    }

    int send_len = agile_modbus_serialize_write_registers(ctx, IAP_PASS, 2, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_registers(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_IAP_PASS, values, rc);
        }
    }
    return 0;
}

uint8_t SRT_IM_W_KEEPREG_CAIL_ZERO(struct _srt_ctr *srt)
{

    uint16_t values[2] = {0};
    // uint16_t Register[10];

    if (srt->fdcan_fdback.dta_length < 6)
    {
        return 0;
    }
    uint8_t vdta_length = (srt->fdcan_fdback.dta_length - 2) / 2;
    for (uint8_t i = 0; i < vdta_length; i++)
    {
        values[i] = srt->fdcan_fdback.data[1 + i] << 8 | srt->fdcan_fdback.data[2 + i]; // 获取设置值
    }

    int send_len = agile_modbus_serialize_write_registers(ctx, CAIL_ZERO, 2, values);
    rs485_send(ctx->send_buf, send_len);
    int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 100, 5);

    if (read_len == 0)
    {
        // LOG_W("Receive timeout.");
        srt_can_packed_u16(srt, DEVICE_RS485_ERR_TIMEOUT, NULL, 0);
    }
    else
    {
        int rc = agile_modbus_deserialize_write_registers(ctx, read_len);

        if (rc < 0) // 如果反馈数据错误
        {
            srt_can_packed_u16(srt, DEVICE_RS485_ERR_FAILURE, NULL, 0);

            LOG_W("Receive failed.");
            if (rc != -1)
                LOG_W("Error code:%d", -128 - rc);
        }
        else
        {
            LOG_I("[---0x%02X---]Register [%d]: 0x%02X", 06, 1, values);
            srt_can_packed_u16(srt, _SRT_IM_W_KEEPREG_CAIL_ZERO, values, rc);
        }
    }
    return 0;
}
